import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.io.IOException;
import java.util.List;

public class Main {
    public static void main(String[] args)throws IOException {
        String json_obj_str = TxtHelper.readTxt("files/"+"paginationObject.txt");
        JSONObject pagination_json_obj=JSON.parseObject(json_obj_str);

        Object pageSize_obj=pagination_json_obj.get("pageSize");
        Object pageNo_obj=pagination_json_obj.get("pageNo");
        System.out.println(pageSize_obj);
        System.out.println(pageNo_obj);
        System.out.println();
        Pagination pagination2=JSON.parseObject(json_obj_str, Pagination.class);
        int pageNo2=pagination2.getPageNo();
        int pageSize2=pagination2.getPageSize();
        System.out.println(pageNo2);
        System.out.println(pageSize2);



        //测试parseArray
        String json_array_str=TxtHelper.readTxt("files/"+"conditionArray.txt");
        List<Condition> conditionList=JSON.parseArray(json_array_str, Condition.class);
        System.out.println(conditionList);
        JSONArray conditionArray=JSON.parseArray(json_array_str);
        System.out.println(json_array_str);

        //解析复杂的JSON对象
        String complexJsonStr = TxtHelper.readTxt("files/complexObject.txt");
        JSONObject complexJsonObj = JSON.parseObject(complexJsonStr);

        //获取简单值
        String description = complexJsonObj.getString("description");
        System.out.println(description);

        //获取嵌套对象
        JSONObject addressObj = complexJsonObj.getJSONObject("address");
        String city = addressObj.getString("city");
        String country = addressObj.getString("country");
        System.out.println("address:" + "[city:" + city + "country:" + country + "]");

        //获取数组
        JSONArray conditionArray1 = complexJsonObj.getJSONArray("conditionList");
        List<Condition> conditions = JSON.parseArray(conditionArray1.toJSONString(), Condition.class);
        System.out.println("[列表]" + "conditions:" + conditions);

        //生成JSON
        Pagination newPagination = new Pagination(10,3);
        String newPaginationJsonStr = JSON.toJSONString(newPagination);
        System.out.println("生成的JSON对象:" + newPaginationJsonStr);


    }
}
